package com.wolves.career.utils;

import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiParam;
import lombok.Data;

import java.util.HashMap;

/**
 * 同一返回类
 *
 * @author 麦翔盛
 * @date 2021年10月19日13:45:18
 */
@Data
public class R {
    @ApiModelProperty(value = "是否执行成功")
    private Boolean success;
    @ApiModelProperty(value = "响应码")
    private Integer code;
    @ApiModelProperty(value = "返回信息")
    private String message;
    @ApiModelProperty(value = "返回数据")
    private HashMap<String, Object> data = new HashMap<>();

    /**
     *  有化构造方法，放置外部new
     */
    private R(){}

    /**
     * 执行成功
     * @return
     */
    public static R ok() {
        R r = new R();
        r.setCode(ResultCode.SUCCESS_CODE);
        r.setSuccess(true);
        r.setMessage("成功");
        return r;
    }

    /**
     * 执行失败
     * @return
     */
    public static R error() {
        R r = new R();
        r.setCode(ResultCode.FAIL_CODE);
        r.setSuccess(false);
        r.setMessage("失败");
        return r;
    }

    /**
     * set方法全部的返回值都为this，为了便于链式编程
     * 类似于 R.success().message().....
     */
    public R success(Boolean success) {
        this.success = success;
        return this;
    }
    public R code(Integer code){
        this.code = code;
        return this;
    }
    public R message(String message){
        this.message = message;
        return this;
    }
    public R data(HashMap<String,Object> data){
        this.data = data;
        return this;
    }
    public R data(String key,Object data){
        this.data.put(key, data);
        return this;
    }


}
